package com.app.micaihu.custom.view.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.app.micaihu.R;
import com.app.micaihu.base.bean.DataBean;
import com.app.micaihu.configure.TypeConstant;
import com.app.micaihu.configure.URIContant;
import com.app.micaihu.custom.components.ParamMap;
import com.app.micaihu.listener.HttpListener;
import com.app.micaihu.manager.UserInforManager;
import com.app.micaihu.utils.DialogUtils;
import com.app.micaihu.utils.HttpUtils;
import com.app.utils.util.ToastUtils;
import com.baidu.mobstat.StatService;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;

/**
 * 举报dialog
 * Created by linkai on 2016/8/4.
 */
public class ReportDialog extends Dialog implements View.OnClickListener {

    private Activity mActivity;


    private String reportMsg;//举报内容
    private String type;//被举报的类型 1 为新闻 2为评论,3为帖子
    private String typeName;//举报类型名字
    private String reportType;//举报类型
    private String reporId;//举报id

    private boolean init;//是否初始化

    public ReportDialog(Activity context) {
        super(context,R.style.CustomDialogStyle);
        mActivity = context;
//        initView();
    }

    public ReportDialog(Activity context, int themeResId) {
        super(context, themeResId);
        mActivity = context;
//        initView();
    }

    /**
     * 初始化布局
     */
    private void initView(){
        View view = LayoutInflater.from(mActivity).inflate(R.layout.dialog_report, null);
        TextView radio = (TextView) view.findViewById(R.id.report_gg);
        //默认选中
        this.reportMsg = radio.getText().toString();
        this.reportType = radio.getTag().toString();
        radio.setOnClickListener(this);
        radio = (TextView) view.findViewById(R.id.report_cbz);
        radio.setOnClickListener(this);
        radio = (TextView) view.findViewById(R.id.report_gs);
        radio.setOnClickListener(this);
        radio = (TextView) view.findViewById(R.id.report_sq);
        radio.setOnClickListener(this);
        radio = (TextView) view.findViewById(R.id.report_mm);
        radio.setOnClickListener(this);
        radio = (TextView) view.findViewById(R.id.report_gdcw);
        radio.setOnClickListener(this);
//        View submit =  view.findViewById(R.id.report_submit);
//        submit.setOnClickListener(this);
        View cancle =  view.findViewById(R.id.report_cancel);
        cancle.setOnClickListener(this);

        setCanceledOnTouchOutside(true);
        try {
            super.show();
        } catch (Exception e) {
            e.printStackTrace();
        }
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setWindowAnimations(R.style.dialogWindowAnim);
        getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT,
                android.view.WindowManager.LayoutParams.WRAP_CONTENT);
        setContentView(view);
    }

    /**
     * 展示举报弹窗
     * @param reportId 被举报的id
     * @param isReportNews 是否为新闻举报 true 表示为新闻举报 false表示是 评论举报
     */
//    public void show(String reportId,boolean isReportNews) {
//        this.reporId = reportId;
//        //1 为新闻举报类型 2为评论举报类型
//        if(isReportNews){
//            this.type = "1";
//        }else{
//            this.type = "2";
//        }
//        if(mActivity != null && !mActivity.isFinishing()){
//            super.show();
//        }
//    }


    /**
     *
     * 展示举报弹窗
     * @param reportId 被举报的id
     *
     * @param reportTypeStatistics  //为统计的 统计类型
     * @param reportType 新闻举报："1":资讯；"2":一级评论;"3":一级帖子; "4":非一级评论;"5":非一级帖子; "6":举报用户
     */
    public void show(String reportId, String reportTypeStatistics, String reportType){
        this.reporId = reportId;
        this.type = reportType;
        //1 为新闻举报类型 2为评论举报类型 3为帖子
        switch (reportTypeStatistics){

            case TypeConstant.PAGE_TOPIC_DETAIL:
            case TypeConstant.PAGE_VIDEO_ORIGINAL:
            case TypeConstant.PAGE_NEWS://新闻
//                this.type = TypeConstant.PAGE_NEWS;
                this.typeName = "新闻";
                break;
            case TypeConstant.PAGE_COMMENT://评论
//                this.type = TypeConstant.PAGE_COMMENT;
                this.typeName = "评论";
                break;
            case TypeConstant.PAGE_POST://
            case TypeConstant.PAGE_MINE_POST://帖子
//                this.type = TypeConstant.PAGE_POST;
                this.typeName = "帖子";
                break;

            case TypeConstant.PAGE_HOME:
//                this.type = TypeConstant.PAGE_HOME;
                this.typeName = "个人主页";

                break;
            default:
//                this.type = TypeConstant.PAGE_NEWS;
                this.typeName = "新闻";
                break;
        }
        if(mActivity != null && !mActivity.isFinishing()){
            //如果没有初始化则去 初始化
            if(!init){
                initView();
                init = true;
            }else{
                try {
                    super.show();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }




    @Override
    public void onClick(View v) {
        if(v == null){
            return;
        }
        switch (v.getId()){
            case R.id.report_gg:
            case R.id.report_gs:
            case R.id.report_sq:
            case R.id.report_cbz:
            case R.id.report_mm:
            case R.id.report_gdcw:
                this.reportMsg = ((TextView)v).getText().toString();
                this.reportType = v.getTag().toString();
                try {
                    this.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                //提交举报
                submitReport();
                break;

//            case R.id.report_submit://提交
//                submitReport();
//                break;
            case R.id.report_cancel://退出
                try {
                    this.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
        }
    }

    /**
     * 提交举报
     */
    private void submitReport(){

        if(!UserInforManager.getInstance().isLogin()){
            UserInforManager.getInstance().toLoginPage(mActivity);
            return;
        }
        if(mActivity != null && !mActivity.isFinishing()){
            this.dismiss();
        }
        ParamMap map = new ParamMap();
        map.putEncode("uid", UserInforManager.getInstance().getUserInfor().getUid());
        map.putEncode("type",type);
        map.putEncode("reportType",reportType);
        map.putEncode("content",reportMsg);
//        if(TextUtils.isEmpty(reporId)){
//            map.putEncode("operatorId", "0");//举报id
//        }else{
            map.putEncode("operatorId", reporId);//举报id

//        }
        HttpUtils.ParmsMapMD5(map);

        HttpListener<DataBean> httpListener = new HttpListener<DataBean>() {

            @Override
            public void onStart() {
                DialogUtils.getInstance().showProgressDialog(mActivity,"正在提交");
            }

            @Override
            public void onError(VolleyError error) {
                DialogUtils.getInstance().dismissProgressDialog();
                ToastUtils.makeText(R.drawable.handle_fail,"举报失败");
            }

            @Override
            public void onSuccess(DataBean response) {
                DialogUtils.getInstance().dismissProgressDialog();
                if(response.noError()){
                    showScuessDialog();
                }else{
                    ToastUtils.makeText(response.getMsg());
                }

            }
        };
        //请求网络
        HttpUtils.addPostRequest(URIContant.NEWS_REPORT, new TypeToken<DataBean>() {
        }.getType(), null, map, httpListener);
        StatService.onEvent(mActivity, "048", typeName+"举报", 1);//统计事件的点击次数
    }


    /**
     * 展示举报成功的dialog
     */
    private void showScuessDialog(){
        if(mActivity == null || mActivity.isFinishing()){
            ToastUtils.makeText(R.drawable.handle_success,"举报成功");
            return;
        }
        HashMap<String,String> map = new HashMap<>();
        map.put("title","举报提交成功");
        map.put("desc", "感谢您的反馈,小编将擦亮眼睛重新审核!");
        map.put("btnPosi","我知道了");
        DialogUtils.getInstance().setOnDialogBtnClick(new DialogUtils.OnDialogBtnClickListener() {
            @Override
            public void onPositiveClick() {
                DialogUtils.getInstance().dismissCustomDialog();
            }

            @Override
            public void onNegativeClick() {

            }
        });
        DialogUtils.getInstance().showCustomDialog(mActivity,map);
//        ToastUtils.makeCustomView(R.layout.dialog_report_result);
    }

}
